import 'package:bxultimate/data/request/base_request.dart';
import 'package:bxultimate/domain/common/AreaXzqh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

typedef XzqhCallback = void Function(String areaCode, String areaName);

class AppXzqhSelect extends StatefulWidget {
  final XzqhCallback onSelect;

  final int level;

  const AppXzqhSelect({super.key, required this.onSelect, required this.level});

  @override
  State<AppXzqhSelect> createState() => _AppXzqhSelectState();
}

class _AppXzqhSelectState extends State<AppXzqhSelect> {
  var tabs = ['请选择'];

  List<AreaXzqh> areaList0 = [];
  List<AreaXzqh> areaList1 = [];
  List<AreaXzqh> areaList2 = [];
  List<AreaXzqh> areaList3 = [];
  List<AreaXzqh> areaList4 = [];

  PageController pageController = PageController(
    initialPage: 0,
  );

  int pageIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getData();
  }

  getData() async {
    areaList0 = await BaseRequest.getAreaXzqhByCode(1);
    setState(() {});
  }

  _buildTabTitle(int index) {
    TextStyle style = TextStyle(fontSize: 16, fontWeight: FontWeight.bold);
    if (pageIndex == index) {
      style = style.copyWith(color: Colors.red);
    }

    return GestureDetector(
      onTap: () {
        pageController.jumpToPage(index);
      },
      child: Container(
        padding: EdgeInsets.all(5),
        child: Text(
          "${tabs[index]}",
          style: style,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 450,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: 30,
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: tabs.length,
              itemBuilder: (context, index) {
                return _buildTabTitle(index);
              },
            ),
          ),
          Expanded(
            child: PageView(
              controller: pageController,
              onPageChanged: (index) {
                pageIndex = index;
                setState(() {});
              },
              children: [
                for (var i = 0; i < tabs.length; i++) _buildTabBarView(i)
              ],
            ),
          )
        ],
      ),
    );
  }

  _buildTabBarView(int tabIndex) {
    var list;
    if (tabIndex == 0) {
      list = areaList0;
    }
    if (tabIndex == 1) {
      list = areaList1;
    }
    if (tabIndex == 2) {
      list = areaList2;
    }
    if (tabIndex == 3) {
      list = areaList3;
    }
    if (tabIndex == 4) {
      list = areaList4;
    }

    return ListView.builder(
      itemBuilder: (context, index) {
        return Padding(
            padding: EdgeInsets.all(12),
            child: GestureDetector(
                onTap: () {
                  loadNext(tabIndex, index);
                },
                child: Text('${list[index].areaName}')));
      },
      itemCount: list.length,
    );
  }

  loadNext(int tabIndex, int index) async {
    var selectList;
    if (tabIndex == 0) {
      selectList = areaList0;
    }
    if (tabIndex == 1) {
      selectList = areaList1;
    }
    if (tabIndex == 2) {
      selectList = areaList2;
    }
    if (tabIndex == 3) {
      selectList = areaList3;
    }

    if (tabIndex == (widget.level - 1)) {
      // 拼接字符串
      tabs.removeAt(tabs.length - 1);
      var areaName = tabs.join("") + selectList[index].areaName!;
      widget.onSelect(selectList[index].areaCode!, areaName);
      Get.back();
      return;
    }

    print("tabIndex: ${tabIndex}");
    tabs.removeRange(tabIndex, tabs.length - 1);
    tabs[tabIndex] = selectList[index].areaName!;
    tabs.add('请选择');
    if (tabIndex == 0) {
      areaList1 =
          await BaseRequest.getAreaXzqhByCode(selectList[index].areaCode);
    }
    if (tabIndex == 1) {
      areaList2 =
          await BaseRequest.getAreaXzqhByCode(selectList[index].areaCode);
    }
    if (tabIndex == 2) {
      areaList3 =
          await BaseRequest.getAreaXzqhByCode(selectList[index].areaCode);
    }

    pageController.jumpToPage(tabIndex + 1);
    setState(() {});
  }
}
